﻿/*----------------------------------------------------------------
//Copyright (C) 2012 赵国伟
//文件功能描述：区域管理客户端脚本
//创建人：赵国伟
//生成时间：2012-04-03 15:52:29
//----------------------------------------------------------------*/
var AjaxServerUrl = "/Manage/AS/ProvinceCityAreaCommon.aspx";
var ModelOfArea;
var AreaID = 0;
var SelectOptionOfProvince = "";
var SelectOptionOfCity = "";
///初始化
function Init() {
    AreaID = 0;
}

///执行搜索；
function GoToSearch() {
    var url = "AreasManage.aspx?";
    url += "keyWords=" + escape($("#txtKeyWords").val().trim());
    url += "&ProvinceID=" + $("#ddlProvinceIDOfSearch").val();
    url += "&CityID=" + $("#ddlCityIDOfSearch").val();
    location.href = url;
}

//执行删除
function Delete(id) {
    if (!confirm("系统提示：您确定要删除该条区域信息？"))
        return;
    AjaxGet(AjaxServerUrl + "?Action=DeleteArea&AreaID=" + id);
}

///检查表单；
function CheckForm() {
    if ($("#ddlProvinceID").val() == "") {
        alert("系统提示：请选择省份！");
        $("#ddlProvinceID").focus();
        return false;
    }
    if ($("#ddlCityID").val() == "") {
        alert("系统提示：请选择城市！");
        $("#ddlCityID").focus();
        return false;
    }
    if ($("#txtAreaName").val() == "") {
        alert("系统提示：请输入区域名称！");
        $("#txtAreaName").focus();
        return false;
    }
    if (!IsNumber($("#txtOrderValue").val())) {
        alert("系统提示：排序值必须为数字！");
        $("#txtOrderValue").focus();
        return false;
    }
    return true;
}

//保存；
function Save() {
    $("#btnSave").attr("disabled", true);
    if (!CheckForm()) {
        $("#btnSave").attr("disabled", false);
        return;
    }

    var data = clone(ModelOfArea);
    data._areaid = AreaID;
    data._provinceid = $("#ddlProvinceID").val();
    data._cityid = $("#ddlCityID").val();
    data._areaname = $("#txtAreaName").val();
    data._ordervalue = $("#txtOrderValue").val();
    data._createdate = "/Date(" + CurrentDateStringToUTC() + ")/";
    data._lastmodifydate = "/Date(" + CurrentDateStringToUTC() + ")/";

    var url = AjaxServerUrl + "?Action=SaveArea";
    AjaxPost(url, "btnSave", data);  //更新
}

//绑定区域信息到录入表单中；
function BindAreaToForm() {
    var url = AjaxServerUrl + "?action=GetArea&AreaID=" + AreaID + "&tttt=" + Math.random();
    $.ajax(
    {
        type: "Get",
        url: url,
        dataType: "json",
        success: function (data) {
            if (data != null) {
                $("#ddlProvinceID").val(data.Province._id);
                QueryCities(data.Province._id, "ddlCityID")
                $("#ddlCityID").val(data.Area._cityid);
                $("#txtAreaName").val(data.Area._areaname);
                $("#txtOrderValue").val(data.Area._ordervalue);
            }
        }
    });
}

///添加界面；
function Add() {
    Init();
    GetPopupDiv(500, 800, "#e2e2e2", "添加区域", GetHtmlForm());
}

//编辑界面；
function Edit(id) {
    Init();
    AreaID = id;
    GetPopupDiv(500, 800, "#e2e2e2", "编辑区域", GetHtmlForm());
    BindAreaToForm();
}

///查询指定省份下的城市信息；
function QueryCities(provinceId, objCity,defaultValue) {
    $("#" + objCity).empty();
    if (provinceId == "") {
        $("#" + objCity).append("<option value=\"\">--选择城市--</option>");
        return;
    }
    var url = AjaxServerUrl + "?action=GetCitiesByProvinceID&ProvinceID=" + provinceId;
    $.ajax(
    {
        type: "Get",
        url: url,
        dataType: "json",
        success: function (data) {
            var options = "";
            if (data != null) {
                
                for (var i = 0; i < data.length; ++i) {
                    if (defaultValue == data[i]._id)
                        options += "<option value=\"" + data[i]._id + "\" Selected>" + data[i]._name + "</option>";
                    else
                        options += "<option value=\"" + data[i]._id + "\">" + data[i]._name + "</option>";
                }
            }
            else {
                options += "<option value=\"\">--无城市信息--</option>";
            }
            $("#" + objCity).append(options);
        }
    });
}



///获取录入界面的HTML;
function GetHtmlForm() {
    var html = new Array();
    html.push("<form>");
    html.push("<table width=\"500\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\" class=\"table\">");
    html.push("  <tr>");
    html.push("    <td colspan=\"2\" class=\"TableHeadRow\">&gt;&gt;区域信息</td>");
    html.push("  </tr>");
    html.push("  <tr>");
    html.push("    <td align=\"right\">所属省份：</td>");
    html.push("    <td><select name=\"ddlProvinceID\" id=\"ddlProvinceID\" onchange=\"QueryCities(this.options[this.selectedIndex].value,'ddlCityID');\">");
    html.push("      <option value=\"\">--选择省份--</option>");
    html.push(SelectOptionOfProvince);
    html.push("    </select>    </td>");
    html.push("  </tr>");
    html.push("  <tr>");
    html.push("    <td align=\"right\">所属城市：</td>");
    html.push("    <td><select name=\"ddlCityID\" id=\"ddlCityID\">");
    html.push("      <option value=\"\">--选择城市--</option>");
    html.push(SelectOptionOfCity);
    html.push("    </select>    </td>");
    html.push("                        </select></td>");
    html.push("  </tr>");
    html.push("  <tr>");
    html.push("    <td width=\"77\" align=\"right\">区域名称：</td>");
    html.push("    <td width=\"423\"><input name=\"txtAreaName\" type=\"text\"  class=\"border6\" id=\"txtAreaName\" size=\"50\">");
    html.push("    <span class=\"red\">(*)</span></td>");
    html.push("  </tr>");
    html.push("  <tr>");
    html.push("    <td align=\"right\">排序值：</td>");
    html.push("    <td><input name=\"txtOrderValue\" type=\"text\" class=\"border6\" id=\"txtOrderValue\" value=\"0\" size=\"5\">");
    html.push("    <span class=\"red\">(*)</span></td>");
    html.push("  </tr>");
    html.push("  <tr>");
    html.push("    <td align=\"right\">&nbsp;</td>");
    html.push("    <td><input name=\"btnSave\" type=\"button\" id=\"btnSave\" value=\" 保 存 \" class=\"btn2\" onclick=\"Save();\">");
    html.push("    <input name=\"btnReset\" type=\"reset\" id=\"btnReset\" value=\" 重 置 \" class=\"btn2\">");
    html.push("    <input name=\"btnClose\" type=\"button\" id=\"btnClose\" value=\"关闭本窗口\" class=\"btn2\" onclick=\"yesOther(false);\"></td>");
    html.push("  </tr>");
    html.push("</table>");
    html.push("</form>");

    return html.join("");

}













